import axios, { AxiosRequestConfig } from 'axios';

axios.defaults.baseURL = process.env.NODE_ENV === 'production' ? process.env.REACT_APP_PROD_SERVER_BASE_URL : process.env.REACT_APP_DEV_SERVER_BASE_URL;
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
axios.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    const newConfig = config;
    const accessoToken = sessionStorage.getItem('access_token');
    if (accessoToken) newConfig.headers.Authorization = `Bearer ${accessoToken}`;
    return newConfig;
  },
  (error: any) => Promise.reject(error),
);
// response拦截器里把AxiosResponse=>AxiosResponse.data
axios.interceptors.response.use(
  (response) => response.data,
  (error) => Promise.reject(error),
);
export default axios;
